#!perl

use strict;
use Stdlog;
use Test::More tests => 30;

# Test 1 -- use Stdlog::Criteria
BEGIN { use_ok('Stdlog::Criteria'); }

my ($crit);


# Test 2
$crit = Stdlog::Criteria->new();
is( ref($crit), 'Stdlog::Criteria',
   "make_formatter returns a coderef" );

# Test 3
is( $crit->({}), 1,
   "Default criteria always returns 1" );


# Test 4 - simple package
$crit = Stdlog::Criteria->new({package => 'Foo'});
is( ref($crit), 'Stdlog::Criteria',
   "check package criteria" );

# Test 5
is( $crit->({package => 'Foo'}), 1,
   "package equality" );

# Test 6
is( $crit->({package => 'Bar'}), 0,
   "package equality, negative" );

# Test 7
is( $crit->({package => 'Foo::Bar'}), 0,
   "sub package equality, negative" );



# Test 8 - regex package 
$crit = Stdlog::Criteria->new({package => '/^Foo::/'});
is( ref($crit), 'Stdlog::Criteria',
   "check package criteria" );

# Test 9
is( $crit->({package => 'Foo::Bar'}), 1,
   "regex sub package equality" );

# Test 10
is( $crit->({package => 'Foo'}), 0,
   "regex package equality, negative" );


# Test 11 - multiple packages
$crit = Stdlog::Criteria->new({package => 'Foo /Bar/ Baz'});
is( ref($crit), 'Stdlog::Criteria',
   "check multiple package criteria" );

# Test 12
is( $crit->({package => 'Foo'}), 1,
   "package equality 1" );

# Test 13
is( $crit->({package => 'BooBarBla'}), 1,
   "regex package equality 2" );

# Test 14
is( $crit->({package => 'Baz'}), 1,
   "sub package equality 3" );

# Test 15
is( $crit->({package => 'Woof'}), 0,
   "sub package equality, negative" );



# Test 16 - simple greater severity
$crit = Stdlog::Criteria->new({severity => 'ERROR+'});
is( ref($crit), 'Stdlog::Criteria',
   "simple greater severity criteria" );

# Test 17
is( !!$crit->({severity => 'ERROR'}), 1,
   "severity baseline" );

# Test 18
is( !!$crit->({severity => 'FATAL'}), 1,
   "severity greater" );

# Test 19
is( $crit->({severity => 'INFO'}), 0,
   "severity less, negative" );


# Test 20 - simple lesser severity
$crit = Stdlog::Criteria->new({severity => 'INFO-'});
is( ref($crit), 'Stdlog::Criteria',
   "simple lesser severity criteria" );

# Test 21
is( !!$crit->({severity => 'INFO'}), 1,
   "severity lesser baseline" );

# Test 22
is( !!$crit->({severity => 'DEBUG'}), 1,
   "severity lesser" );

# Test 23
is( $crit->({severity => 'ERROR'}), 0,
   "severity lesser, negative" );


# Test 24 - multi severity
$crit = Stdlog::Criteria->new({severity => 'DEBUG INFO, ERROR'});
is( ref($crit), 'Stdlog::Criteria',
   "multiple severity criteria" );

# Test 25
is( !!$crit->({severity => 'DEBUG'}), 1,
   "multiple severity baseline 1" );

# Test 26
is( !!$crit->({severity => 'INFO'}), 1,
   "multiple severity baseline 2" );

# Test 27
is( !!$crit->({severity => 'ERROR'}), 1,
   "multiple severity baseline 3" );

# Test 28
is( $crit->({severity => 'FATAL'}), 0,
   "multiple severity negative 1" );

# Test 29
is( $crit->({severity => 'NOTICE'}), 0,
   "multiple severity negative 2" );


# Test 30 - range severity
eval { $crit = Stdlog::Criteria->new({severity => 'INFO-ERROR'}) };
like( $@, qr/Unknown severity config/,
   "bad severity criteria" );
